/**
 * 
 */
package com.thinkgsp.sys.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import com.thinkgsp.jxc.model.Dept;
import com.thinkgsp.jxc.model.InventoryAdd;

/**
 * 系统用户类
 * @author evan
 *
 */
public class SysUser extends SysModel implements Serializable,UserDetails {

	private static final long serialVersionUID = -267500068153312585L;

	private Integer userId;
	private String userName;
	private String password;
	private String confirmPassword;
	private String passwordHint;
	private Boolean accountExpired;
	private Boolean accountLocked;
	private Boolean accountEnabled;
	private Boolean credentialsExpired;
	private Integer empId;
	private List<SysRole> roles = new ArrayList<SysRole>();
	@Deprecated
	private List<InventoryAdd> invadds = new ArrayList<InventoryAdd>();
	@Deprecated
	private Integer currentInvAdd;     //用户当前选择的库房
	
	private List<Dept> depts = new ArrayList<Dept>();
	private Dept logonDept;            //用户当前登录的部门
	
	public SysUser() {
		
	}
	
	public SysUser(String userName) {
		this.userName = userName;
	}
	/**
	 * 用户Id
	 * @return
	 */
	public Integer getUserId() {
		return userId;
	}

	public void setUserId(Integer userId) {
		this.userId = userId;
	}

	/**
	 * 用户名(登录名)
	 * @return
	 */
	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	/**
	 * 密码
	 * @return
	 */
	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	/**
	 * 密码提示
	 * @return
	 */
	public String getPasswordHint() {
		return passwordHint;
	}

	public void setPasswordHint(String passwordHint) {
		this.passwordHint = passwordHint;
	}

	/**
	 * 账户过期
	 * @return
	 */
	public Boolean getAccountExpired() {
		return accountExpired;
	}

	public void setAccountExpired(Boolean accountExpired) {
		this.accountExpired = accountExpired;
	}

	/**
	 * 账户锁定
	 * @return
	 */
	public Boolean getAccountLocked() {
		return accountLocked;
	}

	public void setAccountLocked(Boolean accountLocked) {
		this.accountLocked = accountLocked;
	}

	/**
	 * 账户启用
	 * @return
	 */
	public Boolean getAccountEnabled() {
		return accountEnabled;
	}

	public void setAccountEnabled(Boolean accountEnabled) {
		this.accountEnabled = accountEnabled;
	}

	/**
	 * 关联职员Id
	 * @return
	 */
	public Integer getEmpId() {
		return empId;
	}

	public void setEmpId(Integer empId) {
		this.empId = empId;
	}

	/**
	 * 密码过期
	 * @return
	 */
	public Boolean getCredentialsExpired() {
		return credentialsExpired;
	}

	public void setCredentialsExpired(Boolean credentialsExpired) {
		this.credentialsExpired = credentialsExpired;
	}

	/**
	 * 确认密码，本属性不保存数据库
	 * @return
	 */
	public String getConfirmPassword() {
		return confirmPassword;
	}

	public void setConfirmPassword(String confirmPassword) {
		this.confirmPassword = confirmPassword;
	}
	
	public void addRole(SysRole role) {
		getRoles().add(role);
	}

	/**
	 * 用户拥有的角色
	 * @return
	 */
	public List<SysRole> getRoles() {
		return roles;
	}

	public void setRoles(List<SysRole> roles) {
		this.roles = roles;
	}
	
	/**
	 * 不再使用用户的库房列表，而使用用户的部门列表
	 * @return the invadds
	 */
	@Deprecated
	public List<InventoryAdd> getInvadds() {
		return invadds;
	}

	/**
	 * @param invadds the invadds to set
	 */
	@Deprecated
	public void setInvadds(List<InventoryAdd> invadds) {
		this.invadds = invadds;
		for(InventoryAdd ia : invadds) {
			if(ia.isDefaultInvAdd()) {
				currentInvAdd = ia.getId();
				break;
			}
		}
	}

	/**
	 * 获取当前用户选择的库房
	 * 此属性不再使用，而使用默认部门
	 * @return the currentInvAdd
	 */
	@Deprecated
	public Integer getCurrentInvAdd() {
		return currentInvAdd;
	}

	/**
	 * @param currentInvAdd the currentInvAdd to set
	 */
	@Deprecated
	public void setCurrentInvAdd(Integer currentInvAdd) {
		this.currentInvAdd = currentInvAdd;
	}

	/**
	 * 获取用户可查看的部门列表
	 * @return the depts
	 */
	public List<Dept> getDepts() {
		return depts;
	}

	/**
	 * @param depts the depts to set
	 */
	public void setDepts(List<Dept> depts) {
		this.depts = depts;
	}

	/**
	 * 获取用户当前登录的部门
	 * @return the logonDept
	 */
	public Dept getLogonDept() {
		return logonDept;
	}

	/**
	 * @param logonDept the logonDept to set
	 */
	public void setLogonDept(Dept logonDept) {
		this.logonDept = logonDept;
	}

	/**
	 * 判断该用户是否管理员
	 * ROLE_BOSS,ROLE_ADMIN被视为管理员
	 * @return
	 */
	public boolean isAdministrator() {
		for(SysRole role : roles) {
			if("ROLE_ADMIN".equals(role.getRoleName())) {
				return true;
			}
			if("ROLE_BOSS".equals(role.getRoleName())) {
				return true;
			}
		}
		return false;
	}

	/* (non-Javadoc)
	 * @see com.thinkgsp.sys.model.SysModel#toString()
	 */
	@Override
	public String toString() {
		ToStringBuilder sb = new ToStringBuilder(this, ToStringStyle.DEFAULT_STYLE);
		sb.append("userId",this.userId)
		  .append("userName",this.userName)
		  .append("passwordHint",this.passwordHint)
		  .append("ROLES{");
		for(SysRole role : this.roles) {
			sb.append(role.getRoleName());
		}
		sb.append("}");
		return sb.toString();
	}

	/* (non-Javadoc)
	 * @see com.thinkgsp.sys.model.SysModel#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object o) {
		if(this == o) {
			return true;
		}
		if(!(o instanceof SysUser)) {
			return false;
		}
		final SysUser user = (SysUser)o;
		if(userId != null) {
			return userId.equals(user.getUserId());
		} else {
			return user.getUserId() == null;
		}
	}

	/* (non-Javadoc)
	 * @see com.thinkgsp.sys.model.SysModel#hashCode()
	 */
	@Override
	public int hashCode() {
		return (this.userId != null ? this.userId.hashCode() : 0);
	}

	public Set<GrantedAuthority> getAuthorities() {
		Set<GrantedAuthority> authorities = new LinkedHashSet<GrantedAuthority>();
		authorities.addAll(roles);
		return authorities;
	}

	public String getUsername() {
		return this.userName;
	}

	public boolean isAccountNonExpired() {
		return !this.accountExpired;
	}

	public boolean isAccountNonLocked() {
		return !this.accountLocked;
	}

	public boolean isCredentialsNonExpired() {
		return !this.credentialsExpired;
	}

	public boolean isEnabled() {
		return this.accountEnabled;
	}
}
